package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.en.DictCode;
import com.atguigu.en.HouseStatus;
import com.atguigu.entity.Community;
import com.atguigu.entity.Dict;
import com.atguigu.entity.House;
import com.atguigu.service.CommunityService;
import com.atguigu.service.DictService;
import com.atguigu.service.HouseService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    private static final String PAGE_INDEX = "house/index";
    private static final String PAGE_CREATE = "house/create";
    private static final String PAGE_EDIT = "house/edit";
    private static final String PAGE_DELETE = "redirect:/house";
    private static final String LIST_ACTION = "redirect:/house";
    @Reference
    private HouseService houseService;
    @Reference
    private CommunityService communityService;
    @Reference
    private DictService dictService;

    @RequestMapping
    public String index(@RequestParam Map<String, Object> filters, Model model) {
        //处理pageNum和pageSize为空的情况
        if (filters.get("pageNum") == null || "".equals(filters.get("pageNum"))) {
            filters.put("pageNum", 1);
        }
        if (filters.get("pageSize") == null || "".equals(filters.get("pageSize"))) {
            filters.put("pageSize", 10);
        }
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        //查询所有小区
        saveAllDictToRequestScope(model);

        return PAGE_INDEX;

    }

    /**
     * 查询所有小区及字典里各种列表并且存到请区域
     *
     * @param model
     */

    private void saveAllDictToRequestScope(Model model) {
        //查询所有小区
        List<Community> communityList = communityService.findAll();
        //查询各种初始化列表
        //查询房屋类型集合
        List<Dict> houseTypeList = dictService.findDictListByParentDictCode(DictCode.HOUSETYPE.getMessage());
        //查询房屋楼层集合
        List<Dict> houseFloorList = dictService.findDictListByParentDictCode(DictCode.FLOOR.getMessage());
        //查询建筑风格集合
        List<Dict> houseBuildList = dictService.findDictListByParentDictCode(DictCode.BUILDSTRUCTURE.getMessage());
        //查询装修风格集合
        List<Dict> houseDirectionList = dictService.findDictListByParentDictCode(DictCode.DIRECTION.getMessage());
        //查询房屋用途集合
        List<Dict> houseDecortionList = dictService.findDictListByParentDictCode(DictCode.DECORATION.getMessage());
        //
        List<Dict> houseUserList = dictService.findDictListByParentDictCode(DictCode.HOUSEUSE.getMessage());
        model.addAttribute("communityList", communityList);
        model.addAttribute("houseTypeList", houseTypeList);
        model.addAttribute("floorList", houseFloorList);
        model.addAttribute("buildStructureList", houseBuildList);
        model.addAttribute("directionList", houseDirectionList);
        model.addAttribute("decorationList", houseDecortionList);
        model.addAttribute("houseUseList", houseUserList);
    }

    @RequestMapping("/create")
    public String create(Model model) {
        saveAllDictToRequestScope(model);
        return PAGE_CREATE;

    }

    @PostMapping("/save")
    public String save(House house, Model model) {
        //设置房源的状态:0表示未发布，1表示已发布
        house.setStatus(HouseStatus.UNPUBLISHED.code);
        //1. 调用业务层的方法，保存房源信息
        houseService.insert(house);
        return successPage(model, "新增房源信息成功");
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model) {
        House house = houseService.getById(id);
        model.addAttribute("house", house);
        saveAllDictToRequestScope(model);
        return PAGE_EDIT;
    }

    @PostMapping("/update")
    public String update(House house, Model model) {
        houseService.update(house);
        return successPage(model, "修改房源信息成功！");
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id, Model model) {
        houseService.delete(id);
        return PAGE_DELETE;
    }
    @GetMapping("/publish/{id}/{status}")
    public String publish(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        //发布房源是将房源中的status状态码设置为1
        House house = new House();
        house.setId(id);
        house.setStatus(status);
        houseService.update(house);
        return LIST_ACTION;
    }
}
